package bg.btanev.eightyfour.client.view;

import bg.btanev.eightyfour.client.presenter.SearchTargetPresenter.SearchTargetDisplay;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author B.Tanev
 *
 */
public class SearchTargetView extends Composite implements SearchTargetDisplay {
	private final TextBox firstNameData;
	private final TextBox lastNameData;
	private final TextBox emailData;
	private final Button searchButton;

	public SearchTargetView() {
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("550px");

		final CaptionPanel searchDetailsPanel = new CaptionPanel("Search details");
		verticalPanel.add(searchDetailsPanel);
		searchDetailsPanel.setSize("100%", "106px");

		final Grid searchDetailsContainer = new Grid(3, 2);
		searchDetailsContainer.setCellPadding(3);
		searchDetailsPanel.setContentWidget(searchDetailsContainer);
		searchDetailsContainer.setSize("100%", "");

		final Label firstNameCaption = new Label("First Name");
		searchDetailsContainer.setWidget(0, 0, firstNameCaption);
		firstNameCaption.setWidth("200px");

		firstNameData = new TextBox();
		searchDetailsContainer.setWidget(0, 1, firstNameData);
		firstNameData.setWidth("300px");

		final Label lastNameCaption = new Label("Last Name");
		searchDetailsContainer.setWidget(1, 0, lastNameCaption);
		lastNameCaption.setWidth("200px");

		lastNameData = new TextBox();
		searchDetailsContainer.setWidget(1, 1, lastNameData);
		lastNameData.setWidth("300px");

		final Label emailCaption = new Label("E-Mail");
		searchDetailsContainer.setWidget(2, 0, emailCaption);
		emailCaption.setWidth("200px");

		emailData = new TextBox();
		searchDetailsContainer.setWidget(2, 1, emailData);
		emailData.setWidth("300px");

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");

		final Button clearButton = new Button("Clear");
		horizontalPanel.add(clearButton);
		clearButton.setWidth("100px");
		clearButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				clearView();
			}
		});

		searchButton = new Button("Search");
		horizontalPanel.add(searchButton);
		searchButton.setWidth("100px");
	}

	public HasText getFirstNameData() {
		return firstNameData;
	}

	public HasText getLastNameData() {
		return lastNameData;
	}

	public HasText getEmailData() {
		return emailData;
	}

	public HasClickHandlers getSearchButton() {
		return searchButton;
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void clearView() {
		firstNameData.setText(null);
		lastNameData.setText(null);
		emailData.setText(null);
	}
}
